#!/bin/bash
SERVER_NAME="main"
LOG_FILE="output.log"
PID_FILE="./root/server.pid"
BUILD_DIR="."

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' 

print_status() 
{
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() 
{
    echo -e "${YELLOW}[WARN]${NC} $1"
}

print_error() 
{
    echo -e "${RED}[ERROR]${NC} $1"
}


is_server_running() 
{
    if [ -f "$PID_FILE" ]; then
        local pid=$(cat "$PID_FILE")
        if ps -p "$pid" > /dev/null 2>&1; then
            return 0
        else
            rm -f "$PID_FILE"
        fi
    fi
    return 1
}


start_server() 
{
    if is_server_running; then
        print_warning "服务器已经在运行中 (PID: $(cat "$PID_FILE"))"
        return 1
    fi
    
    print_status "正在编译服务器..."
    cd "$BUILD_DIR"
    make clean && make
    if [ $? -ne 0 ]; then
        print_error "编译失败！"
        return 1
    fi
    
    print_status "启动服务器..."
    nohup ./"$SERVER_NAME" >> "$LOG_FILE" 2>&1 &
    # nohup ./"$SERVER_NAME" &
    local pid=$!
    echo "$pid" > "$PID_FILE"
    
    sleep 2 
    if is_server_running; then
        print_status "服务器启动成功! PID: $pid"
        print_status "日志文件: $LOG_FILE"
    else
        print_error "服务器启动失败，请检查日志: $LOG_FILE"
        rm -f "$PID_FILE"
        return 1
    fi
}


stop_server() 
{
    if is_server_running; then
        local pid=$(cat "$PID_FILE")
        print_status "正在停止服务器 (PID: $pid)..."
        kill -9 "$pid" 2>/dev/null
        rm -f "$PID_FILE"
        print_status "服务器已停止"
    else
        print_warning "服务器未在运行"
    fi
}


restart_server() 
{
    stop_server
    sleep 1
    start_server
}


status_server() 
{
    if is_server_running; then
        local pid=$(cat "$PID_FILE")
        print_status "服务器正在运行中"
        echo -e "  PID: ${BLUE}$pid${NC}"
        echo -e "  启动时间: ${BLUE}$(ps -p "$pid" -o lstart=)${NC}"
        echo -e "  内存使用: ${BLUE}$(ps -p "$pid" -o rss=) KB${NC}"
        echo -e "  日志文件: ${BLUE}$LOG_FILE${NC}"
        

        echo -e "\n最近日志内容:"
        tail -10 "$LOG_FILE" 2>/dev/null || echo "暂无日志内容"
    else
        print_warning "服务器未在运行"
    fi
}


build_server() 
{
    print_status "正在编译服务器..."
    cd "$BUILD_DIR"
    make clean && make
    if [ $? -eq 0 ]; then
        print_status "编译成功！"
    else
        print_error "编译失败！"
        return 1
    fi
}


show_usage() 
{
    echo -e "${BLUE}使用方法:${NC}"
    echo -e "  $0 ${GREEN}start${NC}    - 编译并启动服务器"
    echo -e "  $0 ${GREEN}stop${NC}     - 停止服务器"
    echo -e "  $0 ${GREEN}restart${NC}  - 重启服务器"
    echo -e "  $0 ${GREEN}status${NC}   - 查看服务器状态"
    echo -e "  $0 ${GREEN}build${NC}    - 只编译不启动"
    echo -e "  $0 ${GREEN}help${NC}     - 显示此帮助信息"
}


case "$1" in
    start)
        start_server
        ;;
    stop)
        stop_server
        ;;
    restart)
        restart_server
        ;;
    status)
        status_server
        ;;
    build)
        build_server
        ;;
    help|--help|-h)
        show_usage
        ;;
    *)
        print_error "无效的命令: $1"
        show_usage
        exit 1
        ;;
esac

exit 0